package com.zjy.heath_ms.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjy.heath_ms.mapper.OrderSettingMapper;
import com.zjy.heath_ms.pojo.OrderSetting;
import com.zjy.heath_ms.service.OrderSettingService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 项目名 heath_ms
 * 类名  OrderSettingServiceImpl
 * 作者 lzq
 * 创建时间  2025/5/25 11:25
 */
@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public List<OrderSetting> getOrderSettingByYearMonth(Integer year, Integer month) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("year(order_date) = {0} and month(order_date) = {1}", year, month);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean editOrderNumberByDate(OrderSetting orderSetting) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(orderSetting.getOrderDate());
        String[] split = dateStr.split("-");
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply("year(order_date) = {0} and month(order_date) = {1} and day(order_date) = {2}",
                split[0], split[1], split[2]);

        //更新
        boolean updateRes = baseMapper.update(orderSetting, queryWrapper) > 0;
        //如果未更新成功，那么插入这条数据到数据库
        if (!updateRes) {
            baseMapper.insert(orderSetting);
        }
        return true;
    }

    @Override
    public boolean upload(MultipartFile excelFile) {
        // 导入Excel 文件,并使用EasyPoi来解析
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setSheetNum(1);
        importParams.setKeyIndex(1);
        List<OrderSetting> orderSettings = null;
        try {
            orderSettings = ExcelImportUtil.importExcel(excelFile.getInputStream(), OrderSetting.class, importParams);
        } catch (Exception e) {
            return false;
        }
        for (OrderSetting orderSetting : orderSettings) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dateStr = dateFormat.format(orderSetting.getOrderDate());
            String[] split = dateStr.split("-");

            //更新条件

            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.apply("year(order_date) = {0} and month(order_date) = {1} and day(order_date) = {2}",
                    split[0], split[1], split[2]);

           boolean flag =  baseMapper.update(orderSetting, queryWrapper) > 1;
           if (!flag) {
               baseMapper.insert(orderSetting);
           }
        }
        return true;
    }
}
